﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;

namespace SimpleGame
{
    enum Dept { NatrualSci=0, Engineering, SocialSci, Humanity, Art, Medical, Law, _NullDeptLength };
    public class Building
    {
        public string name;
        public int upgrade_rank;
        public int condition;
        public int month;
        

        protected Texture2D[] images;
        protected bool[][,] imageCollision;
        protected int time;
        protected int frame_gap;//millisecond
        


        protected Vector2 buildingAlign; // 기준점으로부터 얼마나 틀어져있느냐.
        protected Vector2 pos;

        protected Game1 _game;
        protected ContentManager Content;
        protected SpriteBatch spriteBatch;

        public Building(Game1 game, Vector2 pos, int startTime = 0)
        {
            _game = game;
            this.spriteBatch = _game.spriteBatch;
            this.Content = _game.Content;
        }

        public Building(Game1 game, string imageBase, int frame_gap, int imageLen, string name, Vector2 buildingAlign, Vector2 pos, int startTime = 0)
        {
            images = null;
            _game = game;
            this.spriteBatch = _game.spriteBatch;
            this.Content = _game.Content;
            //TODO : imageBase + imageLen으로 로딩
            this.frame_gap = frame_gap;

            this.name = name;
            this.buildingAlign = buildingAlign;
            this.pos = pos;

            this.time = startTime;

            Debug.Assert(images != null && images.Length > 0);
            _setImageCollision();
        }
        protected void _setImageCollision()
        {
            imageCollision = new bool[images.Length][,];

            for (int i = 0; i < images.Length; i++)
            {
                Texture2D image = images[i];
                bool[,] col = new bool[image.Width,image.Height];

                Color[] src = new Color[image.Width*image.Height];
                image.GetData(src);

                for (int y = 0; y < image.Height; y++)
                {
                    for (int x = 0; x < image.Width; x++)
                    {
                        Color color = src[image.Width * y + x];
                        if (color.A > 32)
                        {
                            col[x, y] = true;
                        }
                        else
                        {
                            col[x, y] = false;
                        }
                    }
                }
                

                imageCollision[i] = col;
            }
        }

        public virtual bool Update(GameTime gameTime, Vector2 basicPos, Vector2 mousePos)
        {
            Debug.Assert(imageCollision != null && imageCollision.Length == images.Length);

            int imgIdx = time / frame_gap;
            bool[,] collision = imageCollision[imgIdx];
            int width = collision.GetLength(0);
            int height = collision.GetLength(1);
            Vector2 currPos = basicPos + pos + buildingAlign - new Vector2(width / 2, height);
            currPos = mousePos - currPos;

            if (currPos.X < 0 || currPos.Y < 0)
            {
                return false;
            }
            else if (currPos.X >= width || currPos.Y >= height)
            {
                return false;
            }
            else if (collision[(int)currPos.X, (int)currPos.Y] == false)
            {
                return false;
            }

            return true;
        }

        public bool Draw(GameTime gameTime, Vector2 basicPos)
        {
            Debug.Assert(images != null && images.Length > 0);
            int timeGap = gameTime.ElapsedGameTime.Milliseconds;
            time += timeGap;
            time %= frame_gap * images.Length;

            int imgIdx = time/frame_gap;

            Texture2D image = images[imgIdx];
            Vector2 drawPos = basicPos + pos + buildingAlign - new Vector2(image.Width/2,image.Height);
            spriteBatch.Draw(image, drawPos, Color.White);

            return true;
        }

    }
}
